package com.approval.approvalapp.index.dbsx.qjsp;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approval.QjSplcAdapter;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.QjBean;
import com.approval.approvalapp.bean.QjSqBean;
import com.approval.approvalapp.bean.QjxqBean;
import com.approval.approvalapp.index.dbsx.qjsp.fragment.QjspDbFragment;
import com.approval.approvalapp.index.dbsx.qjsp.fragment.QjspYbFragment;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.DownResponseInterface;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QjspDbXqActivity extends BaseActivity2 {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.name_tv)
    TextView nameTv;
    @Bind(R.id.qjfl_tv)
    TextView qjflTv;
    @Bind(R.id.qjlx_tv)
    TextView qjlxTv;
    @Bind(R.id.kssj_tv)
    TextView kssjTv;
    @Bind(R.id.jssj_tv)
    TextView jssjTv;
    @Bind(R.id.qjts_tv)
    TextView qjtsTv;
    @Bind(R.id.qjsm_tv)
    TextView qjsmTv;
    @Bind(R.id.tv)
    TextView tv;
    @Bind(R.id.refuse_tv)
    LinearLayout refuseTv;
    @Bind(R.id.agree_tv)
    LinearLayout agreeTv;
    @Bind(R.id.rv)
    RecyclerView rv;
    private QjxqBean qjxqBean;
    private String id;
    private String qtgzfw;

    @Override
    public int getLayoutId() {
        return R.layout.activity_qjsp_db_xq;
    }

    @Override
    public void initView() {
        qtgzfw = getIntent().getStringExtra("qtgzfw");
        id = getIntent().getStringExtra("id");
        String qjlx = getIntent().getStringExtra("qjlx");
        qjlxTv.setText(qjlx);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("leave_id", id);

        NetRequsetUtil.getInstance().netRequestPostJson("v1/Center/leave_show", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                qjxqBean = JSON.parseObject(str, QjxqBean.class);
                if (qjflTv == null){
                    return;
                }
                rv.setLayoutManager(new LinearLayoutManager(QjspDbXqActivity.this));
                QjSplcAdapter qjSplcAdapter = new QjSplcAdapter();
                qjSplcAdapter.setListBeanList(qjxqBean.getList());
                rv.setAdapter(qjSplcAdapter);
                qjflTv.setText(qjxqBean.getFen_lei_type());
                kssjTv.setText(qjxqBean.getStart());
                jssjTv.setText(qjxqBean.getEnd());
                qjtsTv.setText(qjxqBean.getDay() + "天");
                qjsmTv.setText(qjxqBean.getContent());
                nameTv.setText(qjxqBean.getName());
                titleTv1.setText(qjxqBean.getName() + "的请假申请");
                if (TextUtils.isEmpty(qjxqBean.getFile_url())) {
                    tv.setVisibility(View.GONE);
                } else {
                    tv.setVisibility(View.VISIBLE);
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final ProgressDialog progressDialog = new ProgressDialog(QjspDbXqActivity.this);
                            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                            progressDialog.setProgressNumberFormat(" ");
                            progressDialog.setTitle("正在下载");
                            progressDialog.setMessage("请稍候...");
                            progressDialog.setProgress(0);
                            progressDialog.setMax(100);
                            progressDialog.setCanceledOnTouchOutside(false);
                            progressDialog.show();

                            NetRequsetUtil.getInstance().downLoadFile(QjspDbXqActivity.this,
                                    qjxqBean.getFile_url(),
                                    qjxqBean.getFile_url().
                                            substring((qjxqBean.getFile_url().lastIndexOf("/") + 1),
                                                    qjxqBean.getFile_url().length())
                                    , new DownResponseInterface() {
                                        @Override
                                        public void successFinish() {
                                            progressDialog.dismiss();

                                        }

                                        @Override
                                        public void progress(int progress) {
                                            progressDialog.setProgress(progress);

                                        }

                                        @Override
                                        public void erro() {
                                            progressDialog.dismiss();

                                        }
                                    });
                        }
                    });
                }

            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {

            }
        });

    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.back_iv, R.id.refuse_tv, R.id.agree_tv})
    public void onViewClicked(View view) {
        JSONObject jsonObject = new JSONObject();
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.refuse_tv:
                if (SpUtil.getString(this, SpUtil.Usertype, "").equals("3")) {
                    jsonObject.put("type", "1");
                } else {
                    jsonObject.put("type", "4");
                }
                if (qjxqBean != null) {
                    jsonObject.put("day", qjxqBean.getDay());
                }
                jsonObject.put("id", id);
                jsonObject.put("name", SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));
                if (SpUtil.getString(this, SpUtil.Usertype, "").equals("1")
                        && QjSpActivity.instance.uriid.equals("1")) {

                    jsonObject.put("qt_gzfw", qtgzfw);
                    jsonObject.put("my_gzfw", SpUtil.getString(this, SpUtil.GZFW1, ""));

                } else if (SpUtil.getString(this, SpUtil.Usertype, "").equals("1")
                        && QjSpActivity.instance.uriid.equals("2")) {

                    jsonObject.put("is_app", "2");

                }
                NetRequsetUtil.getInstance().netRequestPostJson("v1/center/yes_no_leave",
                        jsonObject.toString(), new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                if (QjspDbFragment.instance != null) {
                                    QjspDbFragment.instance.refreshData();
                                }

                                if (QjspYbFragment.instance != null) {
                                    QjspYbFragment.instance.refreshData();
                                }

                                Toast.makeText(QjspDbXqActivity.this, "审核完成", Toast.LENGTH_SHORT).show();
                                QjspDbXqActivity.this.finish();
                            }

                            @Override
                            public void errowithresponse(String str) {

                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
            case R.id.agree_tv:
                if (SpUtil.getString(this, SpUtil.Usertype, "").equals("3")) {
                    jsonObject.put("type", "0");
                } else {
                    jsonObject.put("type", "3");
                }
                if (qjxqBean != null) {
                    jsonObject.put("day", qjxqBean.getDay());
                }
                jsonObject.put("id", id);
                jsonObject.put("name", SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));
                if (SpUtil.getString(this, SpUtil.Usertype, "").equals("1")
                        && QjSpActivity.instance.uriid.equals("1")) {

                    jsonObject.put("qt_gzfw", qtgzfw);
                    jsonObject.put("my_gzfw", SpUtil.getString(this, SpUtil.GZFW1, ""));

                } else if (SpUtil.getString(this, SpUtil.Usertype, "").equals("1")
                        && QjSpActivity.instance.uriid.equals("2")) {

                    jsonObject.put("is_app", "2");

                }
                Log.d("555", jsonObject.toString());
                NetRequsetUtil.getInstance().netRequestPostJson("v1/center/yes_no_leave",
                        jsonObject.toString(), new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                if (QjspDbFragment.instance != null) {
                                    QjspDbFragment.instance.refreshData();
                                }

                                if (QjspYbFragment.instance != null) {
                                    QjspYbFragment.instance.refreshData();
                                }

                                Toast.makeText(QjspDbXqActivity.this, "审核完成", Toast.LENGTH_SHORT).show();
                                QjspDbXqActivity.this.finish();
                            }

                            @Override
                            public void errowithresponse(String str) {

                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
        }
    }
}
